/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.smart.common.enums.SmsTemplateEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.core.factory.SmsFactory;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version SmsUtil.java, v 0.1 2023/9/22 16:20 下午 SmsUtil Exp $$
 */
@Slf4j
public class SmsUtil {

    /**
     * 发送短信
     * @param phones 手机号
     * @param template 短信模板
     * @param messages 模板参数K,V
     * @return
     */
    public static SmsResponse sendMessage(List<String> phones, SmsTemplateEnum template, LinkedHashMap<String, String> messages) {
        if (CollectionUtils.isEmpty(phones)) {
            log.info("联系电话为空...");
            return null;
        }
        phones = phones.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(phones)) {
            log.info("联系电话为空...");
            return null;
        }
        log.info("短信发送 phones={}, sms_template={}", JSON.toJSONString(phones), template);
        return SmsFactory.getSmsBlend(SupplierConstant.ALIBABA).massTexting(phones, template.getTemplateCode(), messages);
    }

    /**
     * 发送短信
     * @param phone 手机号
     * @param template 短信模板
     * @return
     */
    public static SmsResponse sendMessage(String phone, SmsTemplateEnum template) {
        return sendMessage(Lists.newArrayList(phone), template, new LinkedHashMap<>());
    }

    /**
     * 发送短信
     * @param phone 手机号
     * @param template 短信模板
     * @return
     */
    public static SmsResponse sendMessage(String phone, SmsTemplateEnum template, LinkedHashMap<String, String> messages) {
        return sendMessage(Lists.newArrayList(phone), template, messages);
    }

    /**
     * 发送短信
     * @param phones 手机号
     * @param template 短信模板
     * @return
     */
    public static SmsResponse sendMessage(List<String> phones, SmsTemplateEnum template) {
        return sendMessage(phones, template, new LinkedHashMap<>());
    }
}
